package com.wanxi.coupons.service.util;

import com.alibaba.fastjson.JSON;
import com.wanxi.coupon.api.entity.Coupon;
import com.wanxi.coupon.api.entity.CouponStatistic;
import com.wanxi.coupon.api.query.CouponAdminQuery;
import com.wanxi.coupon.api.query.CouponAdminStatisticQuery;
import com.wanxi.coupon.api.query.CouponUserQuery;
import com.wanxi.coupon.api.vo.*;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: EntityCreate
 * Description: 转换实体工具类
 * date: 2022/1/5 18:38
 *
 * @author lxl
 * @version 1.0
 * @since JDK 1.8
 */
@Component
public class EntityCreateUtil {


    /**
     * @Author lxl
     * @Description //TODO admin tdo封装entity层
     * @Date  2021/12/31 11:03
     * @Params  * @param couponAdminTDO
     * @return Coupon
     **/
    public Coupon adminTdoTransitionEntity(CouponAdminTDO couponAdminTDO){
        Assert.notNull(couponAdminTDO.getAdminId(), "管理员id不能为空");
        Assert.notNull(couponAdminTDO.getCouponName(), "优惠券名字不能为空");
        return Coupon
                .builder()
                .adminId(couponAdminTDO.getAdminId())
                .couponName(couponAdminTDO.getCouponName())
                .couponMoney(couponAdminTDO.getCouponMoney())
                .discount(couponAdminTDO.getDiscount())
                .useRange(JSON.toJSONString(couponAdminTDO.getEnterpriseRange()))
                .quota(couponAdminTDO.getQuota())
                .fullReduction(couponAdminTDO.getFullReduction())
                .remark(couponAdminTDO.getRemark())
                .type(couponAdminTDO.getType())
                .startTime(couponAdminTDO.getStartTime())
                .endTime(couponAdminTDO.getEndTime())
                .build();
    }

    /**
     * @Author lxl
     * @Description //TODO admin返回的entity转化为返回前端的实体信息
     * @Date  2021/12/31 16:24
     * @Params  * @param data
     * @return List<CouponAdminVo>
     **/

    public List<CouponAdminVo> entityTransitionAdminVo(List<Coupon> data){

        List<CouponAdminVo> list = new ArrayList();
        for (Coupon a : data) {
            CouponAdminVo couponAdminVo = CouponAdminVo
                    .builder()
                    .couponName(a.getCouponName())
                    .couponMoney(a.getCouponMoney())
                    .discount(a.getDiscount())
                    .endTime(a.getEndTime())
                    .fullReduction(a.getFullReduction())
                    .quota(a.getQuota())
                    .range((List) JSON.parse(a.getUseRange()))
                    .remark(a.getRemark())
                    .startTime(a.getStartTime())
                    .status(a.getStatus())
                    .type(a.getType())
                    .build();
            list.add(couponAdminVo);
        }
        return list;
    }


    /**
     * @Author lxl
     * @Description //TODO admin查询转entity
     * @Date  2021/12/31 16:24
     * @Params  * @param couponQuery
     * @return Coupon
     **/
    public Coupon adminQueryTdoTransitionEntity(CouponAdminQuery couponQuery) {

        return Coupon.builder()
                .couponId(couponQuery.getCouponId())
                .couponName(couponQuery.getCouponName())
                .type(couponQuery.getType())
                .status(couponQuery.getStatus())
                .page(couponQuery.getPage())
                .pageSize(couponQuery.getPageSize())
                .build();

    }


    /**
     * @Author lxl
     * @Description //TODO couponAdminStatisticQuery 转实体
     * @Date  2021/12/31 11:03
     * @Params  * @param couponAdminStatisticQuery
     * @return Coupon
     **/
    public Coupon adminStatisticTransitionEntity(CouponAdminStatisticQuery couponQuery) {

        return Coupon.builder()
                .couponId(couponQuery.getCouponId())
                .couponName(couponQuery.getCouponName())
                .type(couponQuery.getType())
                .status(couponQuery.getStatus())
                .page(couponQuery.getPage())
                .pageSize(couponQuery.getPageSize())
                .build();

    }

    /**
     * @Author lxl
     * @Description //TODO entity转CouponAdminStatisticVo（前端统计展示的值）
     * @Date  2021/12/31 11:03
     * @Params  * @param statistic
     * @return CouponAdminStatisticVo
     **/
    public List<CouponAdminStatisticVo> adminEntityTransitionStatistic(List<CouponStatistic> statistic) {

        if (statistic.isEmpty()){
            return new ArrayList<>();
        }
        List<CouponAdminStatisticVo> list = new ArrayList();
//        初始化值
        int i = 0 ;
        int m = 0 ;

        for (CouponStatistic a : statistic) {

//            初始化值
            if (a.getUseNumber()==null){
                a.setUseNumber(0);
            }
            if (a.getGetNumber()==null){
                a.setGetNumber(0);
            }

            CouponAdminStatisticVo adminStatisticVo = CouponAdminStatisticVo
                    .builder()
                    .couponId(a.getId())
                    .couponMoney(a.getCouponMoney())
                    .discount(a.getDiscount())
                    .fullReduction(a.getFullReduction())
                    .quota(a.getQuota())
                    .type(a.getType())
                    .useNumber(a.getUseNumber())
                    .getNumber(a.getGetNumber())
                    .residue(a.getQuota()-a.getGetNumber())
                    .status(a.getStatus())
                    .build();
            list.add(adminStatisticVo);
        }
        return list;

    }

    /**
     * @Author lxl
     * @Description //TODO 商家封装entity层
     * @Date  2021/12/31 11:03
     * @Params  * @param couponAdminTDO
     * @return Coupon
     **/

    public Coupon enterpriseVoTransitionEntity(CouponEnterpriseTDO data){
        return Coupon
                .builder()
                .enterpriseId(data.getEnterpriseId())
                .couponName(data.getCouponName())
                .couponMoney(data.getCouponMoney())
                .discount(data.getDiscount())
                .useRange(JSON.toJSONString(data.getSkuRange()))
                .quota(data.getQuota())
                .fullReduction(data.getFullReduction())
                .remark(data.getRemark())
                .type(data.getType())
                .startTime(data.getStartTime())
                .endTime(data.getEndTime())
                .build();
    }


    /**
     * @Author lxl
     * @Description //TODO 商家返回的entity转化为返回前端的实体信息
     * @Date  2021/12/31 16:24
     * @Params  * @param data
     * @return List<CouponAdminVo>
     **/

    public List<CouponEnterpriseVo> entityTransitionEnterprisVo(List<Coupon> data){

        List<CouponEnterpriseVo> list = new ArrayList();
        for (Coupon a : data) {
            CouponEnterpriseVo couponEnterpriseVo = CouponEnterpriseVo
                    .builder()
                    .couponName(a.getCouponName())
                    .couponMoney(a.getCouponMoney())
                    .discount(a.getDiscount())
                    .endTime(a.getEndTime())
                    .fullReduction(a.getFullReduction())
                    .quota(a.getQuota())
                    .range((List) JSON.parse(a.getUseRange()))
                    .remark(a.getRemark())
                    .startTime(a.getStartTime())
                    .status(a.getStatus())
                    .type(a.getType())
                    .build();
            list.add(couponEnterpriseVo);
        }
        return list;
    }

    /**
     * @Author lxl
     * @Description //TODO userTDOTransitionCoupon 用户tod转entity和数据库交互
     * @Date  2022/1/6 14:34
     * @Params  * @param coupon
     * @return Coupon
     **/
    public Coupon userTDOTransitionCoupon(CouponUserTDO coupon) {

        return  Coupon.builder()
                .couponId(coupon.getCouponId())
                .userId(coupon.getUserId())
                .build();
    }

    public Coupon userQueryTransitionCoupon(CouponUserQuery coupon) {

        return Coupon.builder()
                .couponId(coupon.getCouponId())
                .userId(coupon.getUserId())
                .type(coupon.getType())
                .status(coupon.getStatus())
                .build();
    }

    public List<CouponUserVo> userCouponVoTransitionEntity(List<Coupon> all) {
        List<CouponUserVo> list = new ArrayList<>();
        all.forEach(a-> list.add(CouponUserVo.builder()
                .couponId(a.getCouponId())
                .couponMoney(a.getCouponMoney())
                .couponName(a.getCouponName())
                .discount(a.getDiscount())
                .endTime(a.getEndTime())
                .fullReduction(a.getFullReduction())
                .quota(a.getQuota())
                .remark(a.getRemark())
                .type(a.getType())
                .status(a.getStatus())
                .startTime(a.getStartTime())
                .build()));

        return list;
    }



}
